package linkedList;

/**
 * 剑指 Offer 24. 反转链表
 * 反转链表
 * 输入一个链表，反转链表后，输出新链表的表头。
 */
public class Solution {
    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);
        ListNode listNode1 = new ListNode(2);
        listNode1.next=new ListNode(3);
        listNode.next=listNode1;
        ListNode change = new Solution().ReverseList(listNode);
        System.out.println(change);
        System.out.println(change.next);
        System.out.println(change.next.next);
    }

    public ListNode ReverseList(ListNode head){
        if (head==null){
            return null;
        }
        ListNode nowNode=head;
        ListNode preNode=null;
        while (nowNode!=null){
            ListNode nextNode = nowNode.next;
            nowNode.next=preNode;
            //为下一次循环做准备
            preNode=nowNode;
            nowNode=nextNode;
        }
        return preNode;
    }

}
